import i18n from '@/i18n'
import Vue from 'vue'

import regExp from './regExp'
import valueControl from './valueControl'
import renderMethod from './renderMethod'

import { createBezierCurvePoints } from './math'

let vueI18n = new Vue({
  i18n
})

const service = {
  regExp,
  valueControl,
  renderMethod,
  createBezierCurvePoints,
  getOptionList (object) {
    let res = []
    for (let key in object) {
      res.push({
        key: parseInt(key) || key === '0' ? parseInt(key) : key,
        value: parseInt(key) || key === '0' ? parseInt(key) : key,
        label: object[key]
      })
    }
    return res
  },
  convertNumber (data, fixedNum) {
    let res = '0.00'
    if (!parseFloat(data)) {
      return res
    } else {
      res = parseFloat(data).toFixed(parseInt(fixedNum) || 0)
      return res
    }
  },
  // 添加千位分隔符
  miliFormat (data) {
    let num = parseInt(data)
    // console.log(data)
    if (!num) return ''
    if (num === 0) return '0'
    return num && num
      .toString()
      .replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  },
  convertTimeToShow (timeString) {
    let res = ''
    try {
      let dateObject = new Date(timeString)

      let year = dateObject.getFullYear()
      let month = dateObject.getMonth() + 1
      if (month < 10) month = `0${month}`
      let date = dateObject.getDate()
      if (date < 10) date = `0${date}`
      let hour = dateObject.getHours()
      if (hour < 10) hour = `0${hour}`
      let minute = dateObject.getMinutes()
      if (minute < 10) minute = `0${minute}`
      let second = dateObject.getSeconds()
      if (second < 10) second = `0${second}`
      res = `${year}-${month}-${date} ${hour}:${minute}:${second}`
      return res
    } catch (e) {
      return new Error('无法解析字符串')
    }
  },
  getEventContent (runningLogList) {
    return runningLogList.map(item => {
      let eventContent = ''
      let reasonStr = ''
      switch (item.msg_type) {
        case 501:
          eventContent = vueI18n.$t(`message.event.501`, { cuAddr: item.data.cu_device })
          break
        case 502:
          eventContent = vueI18n.$t(`message.event.502`, { cuAddr: item.data.cu_device })
          break
        case 506:
          reasonStr = vueI18n.$t(`message.event.reasonStr.506.${item.data.error_code}`)
          eventContent = vueI18n.$t(`message.event.506`, { cuAddr: item.data.cu_device, reasonStr: reasonStr })
          break
        case 519:
          reasonStr = vueI18n.$t(`message.event.reasonStr.519.${item.data.error_code}`)
          eventContent = vueI18n.$t(`message.event.519`, { cuAddr: item.data.caller_cu_device, reasonStr: reasonStr })
          break
        case 520:
          eventContent = vueI18n.$t(`message.event.520`, { callerCuAddr: item.data.caller_cu_device, calledCuAddr: item.data.called_cu_device })
          break
        case 521:
          eventContent = vueI18n.$t(`message.event.521`, {
            cuAddr: item.data.caller_cu_device,
            calledCuAddr: item.data.called_cu_device,
            traffic: item.data.traffic,

            callerSatelliteName: item.data.caller_satellite_name,
            callerTransponderName: item.data.caller_transponder_name,
            sendChBegin: item.data.caller_up_freq_begin,
            sendChEnd: item.data.caller_up_freq_end,
            recvChBegin: item.data.caller_down_freq_begin,
            recvChEnd: item.data.caller_down_freq_end,
            callerBandWidth: item.data.caller_bandwidth,
            sendSpeed: item.data.caller_speed / 1000,
            recvSpeed: item.data.called_speed / 1000,
            sendPower: item.data.caller_power,

            calledSatelliteName: item.data.called_satellite_name,
            calledTransponderName: item.data.called_transponder_name,
            calledSendChBegin: item.data.called_up_freq_begin,
            calledSendChEnd: item.data.called_up_freq_end,
            calledRecvChBegin: item.data.called_down_freq_begin,
            calledRecvChEnd: item.data.called_down_freq_end,
            calledBandWidth: item.data.called_bandwidth,
            calledSendSpeed: item.data.called_speed / 1000,
            calledRecvSpeed: item.data.caller_speed / 1000,
            calledSendPower: item.data.called_power
          })
          break
        case 522:
          eventContent = vueI18n.$t(`message.event.522`, { callerCuAddr: item.data.caller_cu_device, calledCuAddr: item.data.called_cu_device })
          break
        case 523:
          eventContent = vueI18n.$t(`message.event.523`, {
            cuAddr: item.data.cuAddr,
            longitude: item.data.longitude,
            latitude: item.data.latitude,
            height: item.data.height,
            speed: item.data.speed,
            direction: item.data.direction
          })
          break
        case 524:
          if (item.data.state === 0) {
            eventContent = vueI18n.$t(`message.event.5240`, {
              ccuIP: item.data.ccu_ip,
              ccuMangePort: item.data.ccu_manage_port,
              tunnelNo: item.data.tunnel_no,
              tunnelUsage: item.data.type
            })
          } else if (item.data.state === 1) {
            if (item.data.tunnelUsage === 5) {
              eventContent = vueI18n.$t(`message.event.524.5`, {
                ccuIP: item.data.ccu_ip,
                ccuMangePort: item.data.ccu_manage_port,
                tunnelNo: item.data.tunnel_no,
                tunnelUsage: item.data.type
              })
            } else {
              eventContent = vueI18n.$t(`message.event.524.other`, {
                ccuIP: item.data.ccu_ip,
                ccuMangePort: item.data.ccu_manage_port,
                tunnelNo: item.data.tunnel_no,
                tunnelUsage: item.data.type,
                upFreqBegin: item.data.up_freq_begin,
                upFreqEnd: item.data.up_freq_end,
                downFreqBegin: item.data.down_freq_begin,
                downFreqEnd: item.data.down_freq_end
              })
            }
          }
          break
        case 527:
          eventContent = vueI18n.$t(`message.event.527`, {
            device: item.data.ccu_device,
            state: item.data.state
          })
          break
        case 530:
          eventContent = vueI18n.$t(`message.event.530`, {
            cuAddr: item.data.caller_cu_device,
            calledCuAddr: item.data.called_cu_device,
            traffic: item.data.traffic,

            callerSatelliteName: item.data.caller_satellite_name,
            callerTransponderName: item.data.caller_transponder_name,
            sendChBegin: item.data.caller_up_freq_begin,
            sendChEnd: item.data.caller_up_freq_end,
            recvChBegin: item.data.caller_down_freq_begin,
            recvChEnd: item.data.caller_down_freq_end,
            callerBandWidth: item.data.caller_bandwidth,
            sendSpeed: item.data.caller_speed / 1000,
            recvSpeed: item.data.called_speed / 1000,
            sendPower: item.data.caller_power,

            calledSatelliteName: item.data.called_satellite_name,
            calledTransponderName: item.data.called_transponder_name,
            calledSendChBegin: item.data.called_up_freq_begin,
            calledSendChEnd: item.data.called_up_freq_end,
            calledRecvChBegin: item.data.called_down_freq_begin,
            calledRecvChEnd: item.data.called_down_freq_end,
            calledBandWidth: item.data.called_bandwidth,
            calledSendSpeed: item.data.called_speed / 1000,
            calledRecvSpeed: item.data.caller_speed / 1000,
            calledSendPower: item.data.called_power
          })
          break
        case 532:
          eventContent = vueI18n.$t(`message.event.532`, {
            cuAddr: item.data.caller_cu,
            calledCuAddr: item.data.called_cu,

            callerSatelliteName: item.data.satellite_name,
            callerTransponderName: item.data.caller_transponder_name,
            sendChBegin: item.data.caller_up_freq_begin,
            sendChEnd: item.data.caller_up_freq_end,
            recvChBegin: item.data.caller_down_freq_begin,
            recvChEnd: item.data.caller_down_freq_end,
            callerBandWidth: item.data.caller_bandwidth / 1000,
            sendSpeed: item.data.caller_speed / 1000,
            recvSpeed: item.data.called_speed / 1000,
            sendPower: item.data.caller_power,

            calledSatelliteName: item.data.satellite_name,
            calledTransponderName: item.data.called_transponder_name,
            calledSendChBegin: item.data.called_up_freq_begin,
            calledSendChEnd: item.data.called_up_freq_end,
            calledRecvChBegin: item.data.called_down_freq_begin,
            calledRecvChEnd: item.data.called_down_freq_end,
            calledBandWidth: item.data.called_bandwidth / 1000,
            calledSendSpeed: item.data.called_speed / 1000,
            calledRecvSpeed: item.data.caller_speed / 1000,
            calledSendPower: item.data.called_power
          })
          break
        case 533:
          eventContent = vueI18n.$t('message.event.533', { device: item.data.cu_device })
          break
        case 534:
          eventContent = vueI18n.$t('message.event.534', { device: item.data.cu_device })
          break
        case 535:
          eventContent = vueI18n.$t(`message.event.535`, { callerCuAddr: item.data.caller_cu_device, calledCuAddr: item.data.called_cu_device })
          break
        case 536:
          eventContent = vueI18n.$t(`message.event.536`, { data: JSON.stringify(item.data) })
          break
        case 537:
          eventContent = vueI18n.$t(`message.event.537`, { data: JSON.stringify(item.data) })
          break
        case 538:
          eventContent = vueI18n.$t(`message.event.538`, { data: JSON.stringify(item.data) })
          break
        case 539:
          eventContent = vueI18n.$t(`message.event.539`, { data: JSON.stringify(item.data) })
          break
        case 540:
          eventContent = vueI18n.$t(`message.event.540`, { data: JSON.stringify(item.data) })
          break
        case 541:
          eventContent = vueI18n.$t(`message.event.541`, { data: JSON.stringify(item.data) })
          break
        case 542:
          eventContent = vueI18n.$t(`message.event.542`, { data: JSON.stringify(item.data) })
          break
        case 543:
          eventContent = vueI18n.$t(`message.event.543`, { data: JSON.stringify(item.data) })
          break
        case 603:
          eventContent = vueI18n.$t(`message.event.603`, {
            cuAddr: item.data.caller_cu_device,
            calledCuAddr: item.data.called_cu_device,
            traffic: item.data.traffic,

            is_planned: item.data.is_planned,
            ip: item.data.ip,
            dns: item.data.dns,
            mask_len: item.data.mask_len,

            callerSatelliteName: item.data.caller_satellite_name,
            callerTransponderName: item.data.caller_transponder_name,
            sendChBegin: item.data.caller_up_freq_begin,
            sendChEnd: item.data.caller_up_freq_end,
            recvChBegin: item.data.caller_down_freq_begin,
            recvChEnd: item.data.caller_down_freq_end,
            callerBandWidth: item.data.caller_bandwidth,
            sendSpeed: item.data.caller_speed / 1000,
            recvSpeed: item.data.called_speed / 1000,
            sendPower: item.data.caller_power,

            calledSatelliteName: item.data.called_satellite_name,
            calledTransponderName: item.data.called_transponder_name,
            calledSendChBegin: item.data.called_up_freq_begin,
            calledSendChEnd: item.data.called_up_freq_end,
            calledRecvChBegin: item.data.called_down_freq_begin,
            calledRecvChEnd: item.data.called_down_freq_end,
            calledBandWidth: item.data.called_bandwidth,
            calledSendSpeed: item.data.called_speed / 1000,
            calledRecvSpeed: item.data.caller_speed / 1000,
            calledSendPower: item.data.called_power
          })
          break
        case 604:
          reasonStr = vueI18n.$t(`message.event.reasonStr.604.${item.data.reason_code}`)
          eventContent = vueI18n.$t(`message.event.604`, { callerCuAddr: item.data.caller_cu_device, reasonStr })
          break
        case 605:
          eventContent = vueI18n.$t(`message.event.605`, {
            cuAddr: item.data.caller_cu_device,
            calledCuAddr: item.data.called_cu_device,
            traffic: item.data.traffic,

            callerSatelliteName: item.data.caller_satellite_name,
            callerTransponderName: item.data.caller_transponder_name,
            sendChBegin: item.data.caller_up_freq_begin,
            sendChEnd: item.data.caller_up_freq_end,
            recvChBegin: item.data.caller_down_freq_begin,
            recvChEnd: item.data.caller_down_freq_end,
            callerBandWidth: item.data.caller_bandwidth,
            sendSpeed: item.data.caller_speed / 1000,
            recvSpeed: item.data.called_speed / 1000,
            sendPower: item.data.caller_power,

            calledSatelliteName: item.data.called_satellite_name,
            calledTransponderName: item.data.called_transponder_name,
            calledSendChBegin: item.data.called_up_freq_begin,
            calledSendChEnd: item.data.called_up_freq_end,
            calledRecvChBegin: item.data.called_down_freq_begin,
            calledRecvChEnd: item.data.called_down_freq_end,
            calledBandWidth: item.data.called_bandwidth,
            calledSendSpeed: item.data.called_speed / 1000,
            calledRecvSpeed: item.data.caller_speed / 1000,
            calledSendPower: item.data.called_power
          })
          break
        case 606:
          reasonStr = vueI18n.$t(`message.event.reasonStr.606.${item.data.reason_code}`)
          eventContent = vueI18n.$t(`message.event.606`, { cuAddr: item.data.cu_addr, reasonStr })
          break
        case 607:
          eventContent = vueI18n.$t(`message.event.607`, { samState: item.data.sam_state })
          break
        default:
          break
      }
      return {
        ...item,
        msg_type_name: vueI18n.$t(`message.event.msg_type_name.${item.msg_type}`),
        event_content: eventContent
      }
    })
  }
}

export default service
